Early classes of the Java API, such as Vector
, Hashtable
and StringBuffer
, were synchronized to make them
thread-safe. However, synchronization has a significant negative impact on performance, even when using these collections from a single thread.
It is often best to use their non-synchronized counterparts:
-
ArrayList
or LinkedList
instead of Vector
-
Deque
instead of Stack
-
HashMap
instead of Hashtable
-
StringBuilder
instead of StringBuffer
Even when used in synchronized contexts, you should think twice before using their synchronized counterparts, since their usage can be costly. If
you are confident the usage is legitimate, you can safely ignore this warning.
Noncompliant code example
Vector<Cat> cats = new Vector<>();
Compliant solution
ArrayList<Cat> cats = new ArrayList<>();
Exceptions
Usage of these synchronized classes is ignored in the signatures of overriding methods.
@Override
public Vector getCats() {...} // Compliant